
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>types --- 动态类型创建和内置类型名称 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="copy --- 浅层 (shallow) 和深层 (deep) 复制操作" href="copy.html" />
    <link rel="prev" title="weakref --- 弱引用" href="weakref.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/types.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="copy.html" title="copy --- 浅层 (shallow) 和深层 (deep) 复制操作"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="weakref.html" title="weakref --- 弱引用"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">数据类型</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-types">
<span id="types-dynamic-type-creation-and-names-for-built-in-types"></span><h1><a class="reference internal" href="#module-types" title="types: Names for built-in types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code></a> --- 动态类型创建和内置类型名称<a class="headerlink" href="#module-types" title="永久链接至标题">¶</a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/types.py">Lib/types.py</a></p>
<hr class="docutils" />
<p>此模块定义了一些工具函数，用于协助动态创建新的类型。</p>
<p>它还为某些对象类型定义了名称，这些名称由标准 Python 解释器所使用，但并不像内置的 <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> 或 <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 那样对外公开。</p>
<p>最后，它还额外提供了一些类型相关但重要程度不足以作为内置对象的工具类和函数。</p>
<div class="section" id="dynamic-type-creation">
<h2>动态类型创建<a class="headerlink" href="#dynamic-type-creation" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="types.new_class">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">new_class</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">bases=()</em>, <em class="sig-param">kwds=None</em>, <em class="sig-param">exec_body=None</em><span class="sig-paren">)</span><a class="headerlink" href="#types.new_class" title="永久链接至目标">¶</a></dt>
<dd><p>使用适当的元类动态地创建一个类对象。</p>
<p>前三个参数是组成类定义头的部件：类名称，基类 (有序排列)，关键字参数 (例如 <code class="docutils literal notranslate"><span class="pre">metaclass</span></code>)。</p>
<p><em>exec_body</em> 参数是一个回调函数，用于填充新创建类的命名空间。 它应当接受类命名空间作为其唯一的参数并使用类内容直接更新命名空间。 如果未提供回调函数，则它就等效于传入 <code class="docutils literal notranslate"><span class="pre">lambda</span> <span class="pre">ns:</span> <span class="pre">ns</span></code>。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="types.prepare_class">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">prepare_class</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">bases=()</em>, <em class="sig-param">kwds=None</em><span class="sig-paren">)</span><a class="headerlink" href="#types.prepare_class" title="永久链接至目标">¶</a></dt>
<dd><p>计算适当的元类并创建类命名空间。</p>
<p>参数是组成类定义头的部件：类名称，基类 (有序排列) 以及关键字参数 (例如 <code class="docutils literal notranslate"><span class="pre">metaclass</span></code>)。</p>
<p>返回值是一个 3 元组: <code class="docutils literal notranslate"><span class="pre">metaclass,</span> <span class="pre">namespace,</span> <span class="pre">kwds</span></code></p>
<p><em>metaclass</em> 是适当的元类，<em>namespace</em> 是预备好的类命名空间而 <em>kwds</em> 是所传入 <em>kwds</em> 参数移除每个 <code class="docutils literal notranslate"><span class="pre">'metaclass'</span></code> 条目后的已更新副本。 如果未传入 <em>kwds</em> 参数，这将为一个空字典。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>所返回元组中 <code class="docutils literal notranslate"><span class="pre">namespace</span></code> 元素的默认值已被改变。 现在当元类没有 <code class="docutils literal notranslate"><span class="pre">__prepare__</span></code> 方法时将会使用一个保留插入顺序的映射。</p>
</div>
</dd></dl>

<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference internal" href="../reference/datamodel.html#metaclasses"><span class="std std-ref">元类</span></a></dt><dd><p>这些函数所支持的类创建过程的完整细节</p>
</dd>
<dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3115"><strong>PEP 3115</strong></a> - Python 3000 中的元类</dt><dd><p>引入 <code class="docutils literal notranslate"><span class="pre">__prepare__</span></code> 命名空间钩子</p>
</dd>
</dl>
</div>
<dl class="function">
<dt id="types.resolve_bases">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">resolve_bases</code><span class="sig-paren">(</span><em class="sig-param">bases</em><span class="sig-paren">)</span><a class="headerlink" href="#types.resolve_bases" title="永久链接至目标">¶</a></dt>
<dd><p>动态地解析 MRO 条目，具体描述见 <span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0560"><strong>PEP 560</strong></a>。</p>
<p>此函数会在 <em>bases</em> 中查找不是 <a class="reference internal" href="functions.html#type" title="type"><code class="xref py py-class docutils literal notranslate"><span class="pre">type</span></code></a> 的实例的项，并返回一个元组，其中每个具有 <code class="docutils literal notranslate"><span class="pre">__mro_entries__</span></code> 方法的此种对象对象将被替换为调用该方法解包后的结果。 如果一个 <em>bases</em> 项是 <a class="reference internal" href="functions.html#type" title="type"><code class="xref py py-class docutils literal notranslate"><span class="pre">type</span></code></a> 的实例，或它不具有 <code class="docutils literal notranslate"><span class="pre">__mro_entries__</span></code> 方法，则它将不加改变地被包含在返回的元组中。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0560"><strong>PEP 560</strong></a> - 对 typing 模块和泛型类型的核心支持</p>
</div>
</div>
<div class="section" id="standard-interpreter-types">
<h2>标准解释器类型<a class="headerlink" href="#standard-interpreter-types" title="永久链接至标题">¶</a></h2>
<p>此模块为许多类型提供了实现 Python 解释器所要求的名称。 它刻意地避免了包含某些仅在处理过程中偶然出现的类型，例如 <code class="docutils literal notranslate"><span class="pre">listiterator</span></code> 类型。</p>
<p>此种名称的典型应用如 <a class="reference internal" href="functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a> 或 <a class="reference internal" href="functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a> 检测。</p>
<p>以下类型有相应的标准名称定义：</p>
<dl class="data">
<dt id="types.FunctionType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">FunctionType</code><a class="headerlink" href="#types.FunctionType" title="永久链接至目标">¶</a></dt>
<dt id="types.LambdaType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">LambdaType</code><a class="headerlink" href="#types.LambdaType" title="永久链接至目标">¶</a></dt>
<dd><p>用户自定义函数以及由 <a class="reference internal" href="../reference/expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a>  表达式所创建函数的类型。</p>
</dd></dl>

<dl class="data">
<dt id="types.GeneratorType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">GeneratorType</code><a class="headerlink" href="#types.GeneratorType" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> 迭代器对象的类型，由生成器函数创建。</p>
</dd></dl>

<dl class="data">
<dt id="types.CoroutineType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">CoroutineType</code><a class="headerlink" href="#types.CoroutineType" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> 对象的类型，由 <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> 函数创建。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>

<dl class="data">
<dt id="types.AsyncGeneratorType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">AsyncGeneratorType</code><a class="headerlink" href="#types.AsyncGeneratorType" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> 迭代器对象的类型，由异步生成器函数创建。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能.</span></p>
</div>
</dd></dl>

<dl class="data">
<dt id="types.CodeType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">CodeType</code><a class="headerlink" href="#types.CodeType" title="永久链接至目标">¶</a></dt>
<dd><p id="index-3">代码对象的类型，例如 <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 的返回值。</p>
</dd></dl>

<dl class="data">
<dt id="types.MethodType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">MethodType</code><a class="headerlink" href="#types.MethodType" title="永久链接至目标">¶</a></dt>
<dd><p>用户自定义类实例方法的类型。</p>
</dd></dl>

<dl class="data">
<dt id="types.BuiltinFunctionType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">BuiltinFunctionType</code><a class="headerlink" href="#types.BuiltinFunctionType" title="永久链接至目标">¶</a></dt>
<dt id="types.BuiltinMethodType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">BuiltinMethodType</code><a class="headerlink" href="#types.BuiltinMethodType" title="永久链接至目标">¶</a></dt>
<dd><p>内置函数例如 <a class="reference internal" href="functions.html#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len()</span></code></a> 或 <a class="reference internal" href="sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a> 以及内置类方法的类型。 （这里所说的“内置”是指“以 C 语言编写”。）</p>
</dd></dl>

<dl class="data">
<dt id="types.WrapperDescriptorType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">WrapperDescriptorType</code><a class="headerlink" href="#types.WrapperDescriptorType" title="永久链接至目标">¶</a></dt>
<dd><p>某些内置数据类型和基类的方法的类型，例如 <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__init__()</span></code></a> 或 <a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__lt__()</span></code></a>。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="data">
<dt id="types.MethodWrapperType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">MethodWrapperType</code><a class="headerlink" href="#types.MethodWrapperType" title="永久链接至目标">¶</a></dt>
<dd><p>某些内置数据类型和基类的 <em>绑定</em> 方法的类型。 例如 <code class="code docutils literal notranslate"><span class="pre">object().__str__</span></code> 所属的类型。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="data">
<dt id="types.MethodDescriptorType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">MethodDescriptorType</code><a class="headerlink" href="#types.MethodDescriptorType" title="永久链接至目标">¶</a></dt>
<dd><p>某些内置数据类型方法的类型例如 <a class="reference internal" href="stdtypes.html#str.join" title="str.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.join()</span></code></a>。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="data">
<dt id="types.ClassMethodDescriptorType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">ClassMethodDescriptorType</code><a class="headerlink" href="#types.ClassMethodDescriptorType" title="永久链接至目标">¶</a></dt>
<dd><p>某些内置数据类型 <em>非绑定</em> 类方法例如 <code class="docutils literal notranslate"><span class="pre">dict.__dict__['fromkeys']</span></code> 的类型。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="class">
<dt id="types.ModuleType">
<em class="property">class </em><code class="sig-prename descclassname">types.</code><code class="sig-name descname">ModuleType</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">doc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#types.ModuleType" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="../glossary.html#term-module"><span class="xref std std-term">模块</span></a> 的类型。 构造器接受待创建模块的名称及其作为可选项 <a class="reference internal" href="../glossary.html#term-docstring"><span class="xref std std-term">docstring</span></a>。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>如果你希望设置各种由导入控制的属性，请使用 <a class="reference internal" href="importlib.html#importlib.util.module_from_spec" title="importlib.util.module_from_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.module_from_spec()</span></code></a> 来创建一个新模块。</p>
</div>
<dl class="attribute">
<dt id="types.ModuleType.__doc__">
<code class="sig-name descname">__doc__</code><a class="headerlink" href="#types.ModuleType.__doc__" title="永久链接至目标">¶</a></dt>
<dd><p>模块的 <a class="reference internal" href="../glossary.html#term-docstring"><span class="xref std std-term">docstring</span></a>。 默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="types.ModuleType.__loader__">
<code class="sig-name descname">__loader__</code><a class="headerlink" href="#types.ModuleType.__loader__" title="永久链接至目标">¶</a></dt>
<dd><p>用于加载模块的 <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>。 默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 之前该属性为可选项。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="types.ModuleType.__name__">
<code class="sig-name descname">__name__</code><a class="headerlink" href="#types.ModuleType.__name__" title="永久链接至目标">¶</a></dt>
<dd><p>模块的名字</p>
</dd></dl>

<dl class="attribute">
<dt id="types.ModuleType.__package__">
<code class="sig-name descname">__package__</code><a class="headerlink" href="#types.ModuleType.__package__" title="永久链接至目标">¶</a></dt>
<dd><p>一个模块所属的 <a class="reference internal" href="../glossary.html#term-package"><span class="xref std std-term">package</span></a>。 如果模块为最高层级的（即不是任何特定包的组成部分）则该属性应设为 <code class="docutils literal notranslate"><span class="pre">''</span></code>，否则它应设为特定包的名称 (如果模块本身也是一个包则名称可以为 <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a>)。 默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 之前该属性为可选项。</p>
</div>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="types.TracebackType">
<em class="property">class </em><code class="sig-prename descclassname">types.</code><code class="sig-name descname">TracebackType</code><span class="sig-paren">(</span><em class="sig-param">tb_next</em>, <em class="sig-param">tb_frame</em>, <em class="sig-param">tb_lasti</em>, <em class="sig-param">tb_lineno</em><span class="sig-paren">)</span><a class="headerlink" href="#types.TracebackType" title="永久链接至目标">¶</a></dt>
<dd><p>回溯对象的类型，例如 <code class="docutils literal notranslate"><span class="pre">sys.exc_info()[2]</span></code> 中的对象。</p>
<p>请查看 <a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">语言参考</span></a> 了解可用属性和操作的细节，以及动态地创建回溯对象的指南。</p>
</dd></dl>

<dl class="data">
<dt id="types.FrameType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">FrameType</code><a class="headerlink" href="#types.FrameType" title="永久链接至目标">¶</a></dt>
<dd><p>帧对象的类型，例如 <code class="docutils literal notranslate"><span class="pre">tb.tb_frame</span></code> 中的对象，其中 <code class="docutils literal notranslate"><span class="pre">tb</span></code> 是一个回溯对象。</p>
<p>请查看 <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">语言参考</span></a> 了解可用属性和操作的细节。</p>
</dd></dl>

<dl class="data">
<dt id="types.GetSetDescriptorType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">GetSetDescriptorType</code><a class="headerlink" href="#types.GetSetDescriptorType" title="永久链接至目标">¶</a></dt>
<dd><p>使用 <code class="docutils literal notranslate"><span class="pre">PyGetSetDef</span></code> 在扩展模块中定义的对象的类型，例如 <code class="docutils literal notranslate"><span class="pre">FrameType.f_locals</span></code> 或 <code class="docutils literal notranslate"><span class="pre">array.array.typecode</span></code>。 此类型被用作对象属性的描述器；它的目的与 <a class="reference internal" href="functions.html#property" title="property"><code class="xref py py-class docutils literal notranslate"><span class="pre">property</span></code></a> 类型相同，但专门针对在扩展模块中定义的类。</p>
</dd></dl>

<dl class="data">
<dt id="types.MemberDescriptorType">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">MemberDescriptorType</code><a class="headerlink" href="#types.MemberDescriptorType" title="永久链接至目标">¶</a></dt>
<dd><p>使用 <code class="docutils literal notranslate"><span class="pre">PyMemberDef</span></code> 在扩展模块中定义的对象的类型，例如 <code class="docutils literal notranslate"><span class="pre">datetime.timedelta.days</span></code>。 此类型被用作使用标准转换函数的简单 C 数据成员的描述器；它的目的与 <a class="reference internal" href="functions.html#property" title="property"><code class="xref py py-class docutils literal notranslate"><span class="pre">property</span></code></a> 类型相同，但专门针对在扩展模块中定义的类。</p>
<div class="impl-detail compound">
<p><strong>CPython implementation detail:</strong> 在 Python 的其它实现中，此类型可能与 <code class="docutils literal notranslate"><span class="pre">GetSetDescriptorType</span></code> 完全相同。</p>
</div>
</dd></dl>

<dl class="class">
<dt id="types.MappingProxyType">
<em class="property">class </em><code class="sig-prename descclassname">types.</code><code class="sig-name descname">MappingProxyType</code><span class="sig-paren">(</span><em class="sig-param">mapping</em><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType" title="永久链接至目标">¶</a></dt>
<dd><p>一个映射的只读代理。 它提供了对映射条目的动态视图，这意味着当映射发生改变时，视图会反映这些改变。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<dl class="describe">
<dt>
<code class="sig-name descname">key in proxy</code></dt>
<dd><p>如果下层的映射中存在键 <em>key</em> 则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="describe">
<dt>
<code class="sig-name descname">proxy[key]</code></dt>
<dd><p>返回下层的映射中以 <em>key</em> 为键的项。 如果下层的映射中不存在键 <em>key</em> 则引发 <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>。</p>
</dd></dl>

<dl class="describe">
<dt>
<code class="sig-name descname">iter(proxy)</code></dt>
<dd><p>返回由下层映射的键为元素的迭代器。 这是 <code class="docutils literal notranslate"><span class="pre">iter(proxy.keys())</span></code> 的快捷方式。</p>
</dd></dl>

<dl class="describe">
<dt>
<code class="sig-name descname">len(proxy)</code></dt>
<dd><p>返回下层映射中的项数。</p>
</dd></dl>

<dl class="method">
<dt id="types.MappingProxyType.copy">
<code class="sig-name descname">copy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.copy" title="永久链接至目标">¶</a></dt>
<dd><p>返回下层映射的浅拷贝。</p>
</dd></dl>

<dl class="method">
<dt id="types.MappingProxyType.get">
<code class="sig-name descname">get</code><span class="sig-paren">(</span><em class="sig-param">key</em><span class="optional">[</span>, <em class="sig-param">default</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.get" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <em>key</em> 存在于下层映射中则返回 <em>key</em> 的值，否则返回 <em>default</em>。 如果 <em>default</em> 未给出则默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，因而此方法绝不会引发 <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="types.MappingProxyType.items">
<code class="sig-name descname">items</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.items" title="永久链接至目标">¶</a></dt>
<dd><p>返回由下层映射项 (<code class="docutils literal notranslate"><span class="pre">(键,</span> <span class="pre">值)</span></code> 对) 组成的一个新视图。</p>
</dd></dl>

<dl class="method">
<dt id="types.MappingProxyType.keys">
<code class="sig-name descname">keys</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.keys" title="永久链接至目标">¶</a></dt>
<dd><p>返回由下层映射的键组成的一个新视图。</p>
</dd></dl>

<dl class="method">
<dt id="types.MappingProxyType.values">
<code class="sig-name descname">values</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#types.MappingProxyType.values" title="永久链接至目标">¶</a></dt>
<dd><p>返回由下层映射的值组成的一个新视图。</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="additional-utility-classes-and-functions">
<h2>附加工具类和函数<a class="headerlink" href="#additional-utility-classes-and-functions" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="types.SimpleNamespace">
<em class="property">class </em><code class="sig-prename descclassname">types.</code><code class="sig-name descname">SimpleNamespace</code><a class="headerlink" href="#types.SimpleNamespace" title="永久链接至目标">¶</a></dt>
<dd><p>一个简单的 <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> 子类，提供了访问其命名空间的属性，以及一个有意义的 repr。</p>
<p>不同于 <a class="reference internal" href="functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>，对于 <code class="docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> 你可以添加和移除属性。 如果一个 <code class="docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> 对象使用关键字参数进行初始化，这些参数会被直接加入下层命名空间。</p>
<p>此类型大致等价于以下代码:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">SimpleNamespace</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">keys</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">)</span>
        <span class="n">items</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">=</span><span class="si">{!r}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">)</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">(</span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">items</span><span class="p">))</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="vm">__dict__</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> 可被用于替代 <code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">NS:</span> <span class="pre">pass</span></code>。 但是，对于结构化记录类型则应改用 <a class="reference internal" href="collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a>。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="types.DynamicClassAttribute">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">DynamicClassAttribute</code><span class="sig-paren">(</span><em class="sig-param">fget=None</em>, <em class="sig-param">fset=None</em>, <em class="sig-param">fdel=None</em>, <em class="sig-param">doc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#types.DynamicClassAttribute" title="永久链接至目标">¶</a></dt>
<dd><p>在类上访问 __getattr__ 的路由属性。</p>
<p>这是一个描述器，用于定义通过实例与通过类访问时具有不同行为的属性。 当实例访问时保持正常行为，但当类访问属性时将被路由至类的 __getattr__ 方法；这是通过引发 AttributeError 来完成的。</p>
<p>这样就允许有在实例上激活的特征属性，同时又有在类上的同名虚拟属性（一个这样的例子是 Enum）。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.4 新版功能.</span></p>
</div>
</dd></dl>

</div>
<div class="section" id="coroutine-utility-functions">
<h2>协程工具函数<a class="headerlink" href="#coroutine-utility-functions" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="types.coroutine">
<code class="sig-prename descclassname">types.</code><code class="sig-name descname">coroutine</code><span class="sig-paren">(</span><em class="sig-param">gen_func</em><span class="sig-paren">)</span><a class="headerlink" href="#types.coroutine" title="永久链接至目标">¶</a></dt>
<dd><p>此函数可将 <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> 函数转换为返回基于生成器的协程的 <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a>。 基于生成器的协程仍然属于 <a class="reference internal" href="../glossary.html#term-generator-iterator"><span class="xref std std-term">generator iterator</span></a>，但同时又可被视为 <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> 对象兼 <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a>。 不过，它没有必要实现 <a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__await__()</span></code></a> 方法。</p>
<p>如果 <em>gen_func</em> 是一个生成器函数，它将被原地修改。</p>
<p>如果 <em>gen_func</em> 不是一个生成器函数，则它会被包装。 如果它返回一个 <a class="reference internal" href="collections.abc.html#collections.abc.Generator" title="collections.abc.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Generator</span></code></a> 的实例，该实例将被包装在一个 <em>awaitable</em> 代理对象中。 所有其他对象类型将被原样返回。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code> --- 动态类型创建和内置类型名称</a><ul>
<li><a class="reference internal" href="#dynamic-type-creation">动态类型创建</a></li>
<li><a class="reference internal" href="#standard-interpreter-types">标准解释器类型</a></li>
<li><a class="reference internal" href="#additional-utility-classes-and-functions">附加工具类和函数</a></li>
<li><a class="reference internal" href="#coroutine-utility-functions">协程工具函数</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="weakref.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> --- 弱引用</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="copy.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code> --- 浅层 (shallow) 和深层 (deep) 复制操作</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/types.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="copy.html" title="copy --- 浅层 (shallow) 和深层 (deep) 复制操作"
             >下一页</a> |</li>
        <li class="right" >
          <a href="weakref.html" title="weakref --- 弱引用"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="datatypes.html" >数据类型</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>